package com.example.springbootServiceNetwork.demos.web.Controller;

import com.example.springbootServiceNetwork.demos.web.Config.JwtInfo;
import com.example.springbootServiceNetwork.demos.web.Config.Result;
import com.example.springbootServiceNetwork.demos.web.Config.ResultEnum;
import com.example.springbootServiceNetwork.demos.web.Entity.Menu;
import com.example.springbootServiceNetwork.demos.web.Entity.MenuItem;
import com.example.springbootServiceNetwork.demos.web.Service.JwtRedistService;
import com.example.springbootServiceNetwork.demos.web.Service.MenuItemService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;


@RestController
@RequestMapping("/api")
public class MenuItemController {

    @Resource
    private MenuItemService menuItemService;
    @Resource
    private JwtRedistService jwtRedistService;



    /**
     * 字典查询(父类)
     */
    @GetMapping("/queryMenu")
    public Result addDict(@RequestParam("child") String child,@RequestHeader("token") String token){
        Result result =new Result();
        try {
            JwtInfo jwtInfo =jwtRedistService.getUserInfo(token);
            return menuItemService.queryMenu(child,jwtInfo);
        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(ResultEnum.FORBIDDEN.getData());
            result.setData(e.getCause());
        }
        return result;
    }


    /**
     * 新增字典(父类)
     */
    @PostMapping("/addMenu")
    public Result addDict(@RequestBody Menu menu, @RequestHeader("token") String token){
        Result result =new Result();
        try {
            JwtInfo jwtInfo =jwtRedistService.getUserInfo(token);
            return menuItemService.addMenu(menu,jwtInfo);
        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(ResultEnum.FORBIDDEN.getData());
            result.setData(e.getCause());
        }
        return result;
    }

    /**
     * 更新字典(父类)
     */
    @PostMapping("/updateMenu")
    public Result updateDict(@RequestBody Menu menu,@RequestHeader("token") String token){
        Result result =new Result();
        try {
            JwtInfo jwtInfo =jwtRedistService.getUserInfo(token);
            return menuItemService.updateMenu(menu,jwtInfo);
        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(ResultEnum.FORBIDDEN.getData());
            result.setData(e.getCause());
        }
        return result;
    }

    /**
     * 删除字典(父类)
     */
    @PostMapping("/deleteMenu")
    public Result deleteDict(@RequestBody Menu menu,@RequestHeader("token") String token){
        Result result =new Result();
        try {
            JwtInfo jwtInfo =jwtRedistService.getUserInfo(token);
            return menuItemService.deleteMenu(menu,jwtInfo);
        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(ResultEnum.FORBIDDEN.getData());
            result.setData(e.getCause());
        }
        return result;
    }


    /**
     * 查询菜单接口(子类)
     */
    @PostMapping("/queryMenuItem")
    public Result selMenuItem(@RequestBody MenuItem menuItem,@RequestHeader("token") String token){
        Result result =new Result();
        try {
            JwtInfo jwtInfo =new JwtInfo();
            if (!(token.isEmpty())){
                 jwtInfo =jwtRedistService.getUserInfo(token);
            }else {
                jwtInfo.setPass(false);
            }
            return menuItemService.queryMenuItem(menuItem,jwtInfo);
        }catch (Exception e){
            result.setCode(ResultEnum.FORBIDDEN.getCode());
            result.setMsg(ResultEnum.FORBIDDEN.getData());
        }
        return result;
    }


    /**
     * 新增字典(子类)
     */
    @PostMapping("/addMenuItem")
    public Result addDictItem(@RequestBody MenuItem menuItem, @RequestHeader("token") String token){
        Result result =new Result();
        try {
            JwtInfo jwtInfo =jwtRedistService.getUserInfo(token);
            return menuItemService.addMenuItem(menuItem,jwtInfo);
        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(ResultEnum.FORBIDDEN.getData());
            result.setData(e.getCause());
        }
        return result;
    }

    /**
     * 更新字典(子类)
     */
    @PostMapping("/updateMenuItem")
    public Result updateDictItem(@RequestBody MenuItem menuItem,@RequestHeader("token") String token){
        Result result =new Result();
        try {
            JwtInfo jwtInfo =jwtRedistService.getUserInfo(token);
            return menuItemService.updateMenuItem(menuItem,jwtInfo);
        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(ResultEnum.FORBIDDEN.getData());
            result.setData(e.getCause());
        }
        return result;
    }

    /**
     * 删除字典(子类)
     */
    @PostMapping("/deleteMenuItem")
    public Result deleteDictItem(@RequestBody MenuItem menuItem,@RequestHeader("token") String token){
        Result result =new Result();
        try {
            JwtInfo jwtInfo =jwtRedistService.getUserInfo(token);
            return menuItemService.deleteMenuItem(menuItem,jwtInfo);
        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(ResultEnum.FORBIDDEN.getData());
            result.setData(e.getCause());
        }
        return result;
    }




}
